﻿using BUSINESS;
using DataAccessObject;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace GUI.LopHoc
{
    /// <summary>
    /// Interaction logic for GUILopHoc.xaml
    /// </summary>
    public partial class GUILopHoc : Window
    {
        int _noOfErrors = 0;
        private string filter = string.Empty;
        private List<LOPHOC> listLopHoc;
        private List<LOPHOC> listCurrentLopHoc = new List<LOPHOC>();
        private List<PHANCONG> listPhanCong = new List<PHANCONG>();
        public GUILopHoc()
        {
            InitializeComponent();
            this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            InitializeControl();
        }

        public string Filter
        {
            get
            {
                return this.filter.ToUpperInvariant();
            }
            set
            {
                filter = value;
                var query = from data in listLopHoc
                            where data.TenLopHoc.ToUpperInvariant().Contains(Filter)
                            select data;
                listCurrentLopHoc.Clear();
                listCurrentLopHoc.AddRange(query.ToList());
                txtListLopHoc.Items.Refresh();
            }
        }

        private void InitializeControl()
        {
            txtInputKhoiLop.ItemsSource = KhoiLopBUS.getAllKhoiLop();
            if (txtInputKhoiLop.Items.Count > 0) txtInputKhoiLop.SelectedIndex = 0;
            txtInputNamHoc.ItemsSource = NamHocBUS.getAllNamHoc();
            if (txtInputNamHoc.Items.Count > 0) txtInputNamHoc.SelectedIndex = 0;
            //////////////////////////////////////////////////////
            listLopHoc = LopHocBUS.getAllLopHoc();
            listCurrentLopHoc.AddRange(listLopHoc);
            txtListLopHoc.ItemsSource = listCurrentLopHoc;
            /////////////////////////////////////////////////////
            tbPhanCong.ItemsSource = listPhanCong;
            ////////////////////////////////////////////////////
            txtListBanHoc.ItemsSource = BanHocBUS.getAllBanLopHoc();
            if (txtListBanHoc.Items.Count > 0) txtListBanHoc.SelectedIndex = 0;
            onSave();
        }

        private void btnChonGiaoVien_Click(object sender, RoutedEventArgs e)
        {
            GUIChonGiaoVien guiChonGiaoVien = new GUIChonGiaoVien(txtInputNamHoc.SelectedItem as NAMHOC);
            guiChonGiaoVien.ShowDialog();
            txtInputGiaoVien.DataContext = guiChonGiaoVien.GiaoVien;
        }

        private void btnCapNhat_Click(object sender, RoutedEventArgs e)
        {
            int index = txtListLopHoc.SelectedIndex;
            LOPHOC lophoc = txtInputTenLopHoc.DataContext as LOPHOC;
            lophoc.TenLopHoc = txtInputTenLopHoc.Text;
            NAMHOC namhoc = txtInputNamHoc.SelectedItem as NAMHOC;
            KHOILOP khoilop = txtInputKhoiLop.SelectedItem as KHOILOP;
            GIAOVIEN giaovien = txtInputGiaoVien.DataContext as GIAOVIEN;
            try
            {
                lophoc = LopHocBUS.updateLopHoc(lophoc, namhoc, khoilop, giaovien);
                ////////////////////////////////////////////////////////////
                listLopHoc[index] = lophoc;
                listCurrentLopHoc.Clear();
                listCurrentLopHoc.AddRange(listLopHoc);
                txtListLopHoc.Items.Refresh();
            }
            catch { }
        }

        private void txtListLopHoc_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            LOPHOC lophoc = txtListLopHoc.SelectedItem as LOPHOC;
            if (lophoc == null) return;
            lophoc = LopHocBUS.getFullLopHoc(lophoc);
            txtInputTenLopHoc.DataContext = lophoc;
            txtInputTenLopHoc.Text = lophoc.TenLopHoc;
            txtInputKhoiLop.SelectedItem = (txtInputKhoiLop.ItemsSource as List<KHOILOP>).FirstOrDefault(p => p.KhoiLopId == lophoc.KHOILOP.KhoiLopId);
            txtInputGiaoVien.DataContext = lophoc.GIAOVIENCHUNHIEM;
            txtInputNamHoc.SelectedItem = (txtInputNamHoc.ItemsSource as List<NAMHOC>).FirstOrDefault(p => p.NamHocId == lophoc.NAMHOC.NamHocId);
            onUpdateState();
            loadPhanCong();
        }

        private void Add_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = _noOfErrors == 0;
            e.Handled = true;
        }

        private void Add_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            NAMHOC namhoc = txtInputNamHoc.SelectedItem as NAMHOC;
            KHOILOP khoilop = txtInputKhoiLop.SelectedItem as KHOILOP;
            GIAOVIEN giaovien = txtInputGiaoVien.DataContext as GIAOVIEN;
            BANLOPHOC banhoc = txtListBanHoc.SelectedItem as BANLOPHOC;

            LOPHOC lophoc = new LOPHOC()
            {
                TenLopHoc = txtInputTenLopHoc.Text,
            };

            try
            {
                LopHocBUS.insertLopHoc(lophoc, namhoc, khoilop, giaovien, banhoc);
                listCurrentLopHoc.Add(lophoc);
                txtListLopHoc.Items.Refresh();
                txtInputTenLopHoc.Text = "";
                txtInputGiaoVien.Text = "";
            }
            catch { }
        }

        private void Validation_Error(object sender, ValidationErrorEventArgs e)
        {
            if (e.Action == ValidationErrorEventAction.Added)
                _noOfErrors++;
            else
                _noOfErrors--;
        }

        private void onUpdateState()
        {
            btnCapNhat.IsEnabled = true;
            btnLuuMoi.IsEnabled = false;
        }

        private void onSave()
        {
            btnCapNhat.IsEnabled = false;
            btnLuuMoi.IsEnabled = true;
        }

        private void btnThemMoi_Click(object sender, RoutedEventArgs e)
        {
            onSave();
            txtInputTenLopHoc.Text = "";
            txtInputGiaoVien.Text = "";
        }

        private void loadPhanCong()
        {
            LOPHOC lophoc = txtListLopHoc.SelectedItem as LOPHOC;
            listPhanCong.Clear();
            listPhanCong.AddRange(PhanCongBUS.getPhanCongByLopHoc(lophoc));
            tbPhanCong.Items.Refresh();
        }

        private void tbPhanCong_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            PHANCONG phancong = tbPhanCong.SelectedItem as PHANCONG;
            if (phancong == null) return;
            txtPhanCongMonHoc.DataContext = phancong.CHITIETMONHOC.MONHOC;
            txtPhanCongGiaoVien.ItemsSource = GiaoVienBUS.getGiaoVienByChuyenMon(phancong.CHITIETMONHOC);
            txtPhanCongGiaoVien.Items.Refresh();
        }

        private void txtPhanCongXacNhan_Click(object sender, RoutedEventArgs e)
        {
            PHANCONG phancong = tbPhanCong.SelectedItem as PHANCONG;
            if (phancong == null) return;
            GIAOVIEN giaovien = txtPhanCongGiaoVien.SelectedItem as GIAOVIEN;
            if (giaovien == null) return;
            var _phancong = PhanCongBUS.updatePhanCong(phancong, giaovien);
            int index = tbPhanCong.SelectedIndex;
            if (index < 0) return;
            listPhanCong[index] = _phancong;
            tbPhanCong.Items.Refresh();
        }
    }
}
